﻿using GameFramework.Event;
using System;
using TowerDefence_Zombie.Data;

namespace TowerDefence_Zombie
{
    public class GuideCondition_CanBuyPos : GuideCondition_Base
    {
        public GuideCondition_CanBuyPos(DRGuideCondition conditionInfo, Action conitionCompleteClick)
            : base(conditionInfo, conitionCompleteClick) { }

        public override void StartCondition()
        {
            base.StartCondition();
            GameEntry.Event.Subscribe(ChangeCurrencyEventArgs.EventId, OnCurrencyChange);
            if (CheckPositionIsMeet())
            {
                IsCompleteFlag = true;
            }
        }

        private void OnCurrencyChange(object sender, GameEventArgs e)
        {
            ChangeCurrencyEventArgs ne = (ChangeCurrencyEventArgs)e;
            if (ne == null)
                return;

            if (ne.MoneyId == 109 || ne.MoneyId == 103)
            {
                IsCompleteFlag = CheckPositionIsMeet();
            }
        }

        private bool CheckPositionIsMeet()
        {
            var dataPlayer = GameEntry.Data.GetData<DataPlayer>();
            var hoerUpgradeData = GameEntry.Data.GetData<HeroUpgradeData>();
            int posNextLevel = dataPlayer.GetTowerPositionLevel() + 1;

            if (posNextLevel >= hoerUpgradeData.GetTowerPositionMaxLevel())
            {
                return true;
            }

            hoerUpgradeData.GetBuilderUpgradeMaterials(posNextLevel, out var specialMaterials, out var commonMaterials);
            var currencyIds = new int[] { 109, 103 };
            var currencyNums = new long[] { specialMaterials, commonMaterials };

            return dataPlayer.CheckCurrencyIsMeet(currencyIds, currencyNums);
        }

        public override void Dispose()
        {
            GameEntry.Event.Unsubscribe(ChangeCurrencyEventArgs.EventId, OnCurrencyChange);
            base.Dispose();
        }
    }
}
